<?php
class Model_PasswordMail
{
    //TODO find solution to get $this->view->translate() method
    public function sendMailNewPass ($email, $password, $view)
    {
        //create letter and send it with new pass, and write that pass to DB
        $mail = new Zend_Mail('utf-8');
        $mail->setBodyHtml(
        '<p>' . $view->view->translate('New pass body text') . '</p><p><b>' .
         $password .
         '</b></p><a href="http://www.people-z.ru">http://www.people-z.ru</a><br/><br/><p>' .
         $view->view->translate("Support") . ': support@people-z.ru</p>');
        $mail->setFrom('bot@people-z.ru', '«Великий Народ»');
        $mail->addTo($email);
        $mail->setSubject($view->view->translate('New password'));
        $mail->send();
    }
    //generate random password
    public function generatePassword ($length = 6)
    {
        list ($usec, $sec) = explode(' ', microtime());
        srand((float) $sec + ((float) $usec * 100000));
        $validchars = "0123456789abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $password = "";
        $counter = 0;
        while ($counter < $length) {
            $actChar = substr($validchars, rand(0, strlen($validchars) - 1), 1);
            // All character must be different
            if (! strstr($password, $actChar)) {
                $password .= $actChar;
                $counter ++;
            }
        }
        return $password;
    }
}